// pages/payOrder/payOrder.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
//index.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //优惠卷id
    couponId: '',
    //优惠卷折扣
    dis: '',
    //优惠卷数量
    discountNum: 0,
    //优惠金额
    discount: 0,
    //订单详情
    orderDetail: {},
    chooseImg: '../../img/xz@3x.png',
    noChooseImg: '../../img/wxz@3x.png',
    //订单编号
    sn: '',
    method: 'balance'
  },

  chooseBalance: function () {
    this.setData({
      chooseImg: '../../img/xz@3x.png',
      noChooseImg: '../../img/wxz@3x.png',
      method: 'balance'
    })
  },
  chooseWx: function () {
    this.setData({
      chooseImg: '../../img/wxz@3x.png',
      noChooseImg: '../../img/xz@3x.png',
      method: 'wechat'
    })
  },
  selectCoupon: function () {
    wx.redirectTo({
      url: '../coupon/coupon?sn=' + this.data.sn + '&parkType=' + this.data.orderDetail.parkType + '&parkingName=' + this.data.orderDetail.parkingName
    })
  },
  confirmPay: function () {
    this.pay();
  },
  //获取订单详情
  orderInfo: function () {
    var that = this
    util.req('/order/detail', { sn: this.data.sn }, function (success) {
      console.log(success)
      if (success.resultCode == 200) {
        success.resultContent.reduce = success.resultContent.reduce || 0;
        success.resultContent.couponDiscount = success.resultContent.couponDiscount || 0;
        success.resultContent.reduce += success.resultContent.couponDiscount;
        if (success.resultContent.couponDiscount != 0) {
          that.setData({
            discountNum: 1
          })
        }
        if (that.data.couponId != '' && that.data.couponId != null) {
          console.log('有优惠卷')
          console.log(that.data.dis);
          //总价
          var amount;
          console.log(success.resultContent.couponDiscount)
          if (success.resultContent.couponDiscount != 0) {
            amount = ((success.resultContent.amount + success.resultContent.couponDiscount) * that.data.dis / 10).toFixed(2);
            console.log(amount);
          } else {
            amount = (success.resultContent.amount * that.data.dis / 10).toFixed(2);
          }
          //优惠金额
          var reduceSum = (success.resultContent.amount - amount + success.resultContent.reduce).toFixed(2);
          console.log(reduceSum);
          var orderDetail = {
            finishDate: util.formatTime(success.resultContent.finishDate),
            orderDuration: util.formatSeconds(success.resultContent.orderDuration),
            useDate: util.formatTime(success.resultContent.useDate),
            uid: success.resultContent.uid,
            amount: amount,
            reduceSum: reduceSum,
            parkType: success.resultContent.parkType,
            number: success.resultContent.number,
            parkingName: success.resultContent.parkingName
          }
        }
        else {
          console.log('没有优惠卷')
          var orderDetail = {
            finishDate: util.formatTime(success.resultContent.finishDate),
            orderDuration: util.formatSeconds(success.resultContent.orderDuration),
            useDate: util.formatTime(success.resultContent.useDate),
            uid: success.resultContent.uid,
            amount: success.resultContent.amount,
            reduceSum: success.resultContent.reduce,
            parkType: success.resultContent.parkType,
            number: success.resultContent.number,
            parkingName: success.resultContent.parkingName
          }
        }
        that.setData({
          orderDetail: orderDetail
        })

        console.log(orderDetail)
      }
    })
  },
  //选择支付方式
  pay: function () {
    var that = this
    util.req('/order/pay', { orderSn: this.data.sn, memberId: globalData.memberId, method: this.data.method, couponId: this.data.couponId }, function (success) {
      console.log(success)
      if (success.resultCode == 200) {
        if (that.data.method == 'wechat') {
          wx.requestPayment({
            'timeStamp': success.resultContent.timeStamp,
            'nonceStr': success.resultContent.nonceStr,
            'package': success.resultContent.package,
            'signType': 'MD5',
            'paySign': success.resultContent.sign,
            'success': function (res) {
              wx.redirectTo({
                url: '../index/index'
              })
            },
            'fail': function (res) {
              wx.showToast({
                title: '用户取消',
                icon: 'none'
              })
            }
          })
        } else {
          wx.showToast({
            title: '支付成功',
            icon: 'none',
            duration: 500
          })
          // 获取用户信息
          util.req('/member/detail', { 'memberId': globalData.memberId }, function (res) {
            console.log(res);
            if (res.resultCode == 200) {
              // 格式化余额，保留小数点后两位
              res.resultContent.balance = (res.resultContent.balance).toString();
              if (res.resultContent.balance.indexOf('.') == -1) {
                res.resultContent.balance = res.resultContent.balance + '.00';
              } else if (res.resultContent.balance.charAt(res.resultContent.balance.length - 2) == '.') {
                res.resultContent.balance = res.resultContent.balance + '0'
              }
              globalData.userInfo = res.resultContent;
            } else if (res.resultCode == 400) {
              wx.showToast({
                title: res.resultMsg,
                icon: 'none'
              })
            } else {
              wx.showToast({
                title: '获取用户信息错误！',
                icon: 'none'
              })
            }
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '../index/index',
            })
          }, 500)

        }
      } else {
        wx.showToast({
          title: success.resultMsg,
          icon: 'none'
        })
      }
    }
    )
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //获取传递的sn
    console.log(options)
    if (options != undefined) {
      if (options.couponId != null && options.couponId != '') {
        this.setData({
          sn: options.sn,
          couponId: options.couponId,
          dis: options.discountNum,
          discountNum: 1
        })
        console.log(this.data.couponId)
      } else {
        this.setData({
          sn: options.sn
        })
      }

      //设定测试数据
      // this.setData({
      //   sn: '10000021528185741'
      // })
      this.orderInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})